package com.cpkso.buss.backend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.cpkso.buss.backend.security.HeaderInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.joda.JodaModule;

/**
 * 拦截器 java config
 * @author wrzhxy@qq.com
 * @date 2017年11月21日
 */
@Configuration
@EnableWebMvc
//@EnableJpaRepositories(repositoryBaseClass = StatisticationDaoImpl.class)
public class WebConfig implements WebMvcConfigurer {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HeaderInterceptor());
//        registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
//        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
    }
	/**
	 * TODO 标上 干啥用？
	 * @return
	 */
	@Bean
    public Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean() {
        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.registerModule(new JodaModule());
        Hibernate5Module hm = new Hibernate5Module();

        hm.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, false);

        objectMapper.registerModule(hm);

        Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean();

        jackson2ObjectMapperFactoryBean.setObjectMapper(objectMapper);

        jackson2ObjectMapperFactoryBean.setIndentOutput(false);

        return jackson2ObjectMapperFactoryBean;
    }


    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

        mappingJackson2HttpMessageConverter
                .setObjectMapper(jackson2ObjectMapperFactoryBean().getObject());

        return mappingJackson2HttpMessageConverter;
    } 
}
